package pl.th.permission.demo.example;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import pl.th.permission.annotation.NeedPermission;
import pl.th.permission.annotation.OnPermissionDenied;
import pl.th.permission.annotation.OnPermissionGranted;

public class LocationActivity extends BaseActivity{

    private static final String TAG = "LocationActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestLocationPermission();
    }

    @NeedPermission(
            requestCode = 200,
            permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION
            }
    )
    private void requestLocationPermission() {

    }

    @OnPermissionGranted(
            requestCode = 200,
            permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            }
    )
    @SuppressLint("MissingPermission")
    private void requestLocation() {
        Toast.makeText(this, "获取前台位置", Toast.LENGTH_SHORT).show();
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria,true);
        locationManager.requestLocationUpdates(provider, 1000L, 0f, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                Log.i(TAG, "onLocationChanged: latitude = " + location.getLatitude() + ", longitude = " + location.getLongitude());
            }
        });
    }

    @OnPermissionGranted(
            requestCode = 200,
            permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION
            }
    )
    @SuppressLint("MissingPermission")
    private void requestBackgroundLocation() {
        Toast.makeText(this, "获取前后台位置", Toast.LENGTH_SHORT).show();
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria,true);
        locationManager.requestLocationUpdates(provider, 1000L, 0f, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                Log.i(TAG, "onLocationChanged: latitude = " + location.getLatitude() + ", longitude = " + location.getLongitude());
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                LocationListener.super.onStatusChanged(provider, status, extras);
            }
        });
    }

    @OnPermissionDenied(
            requestCode = 200,
            permissions = {
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION
            }
    )
    private void denyLocationPermission() {
        Toast.makeText(this, "禁止前后台获取位置", Toast.LENGTH_SHORT).show();
    }

    @OnPermissionDenied(
            requestCode = 200,
            permissions = {
                    Manifest.permission.ACCESS_BACKGROUND_LOCATION
            }
    )
    private void denyBackgroundLocationPermission() {
        Toast.makeText(this, "禁止后台获取位置", Toast.LENGTH_SHORT).show();
    }
}
